package com.cm.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cm.common.utils.COSStorage;
import com.cm.common.utils.StringUtil;
import com.cm.goods.entity.Brand;
import com.cm.common.entity.Goods;
import com.cm.goods.entity.Type;
import com.cm.goods.entity.dto.GoodsKeyWordDTO;
import com.cm.goods.entity.vo.GoodsVO;
import com.cm.goods.mapper.GoodsMapper;
import com.cm.goods.mapper.TypeMapper;
import com.cm.goods.service.GoodsService;
import com.cm.goods.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @类 名:GoodsServiceImpl </br>
 * @描 述:<br>
 * @日 期:2020/9/23 18:00 </br>
 * @作 者:小兰同学</br>
 * @版 本：1.0.0
 * @since JDK 1.8
 */
@Service
public class GoodsServiceImpl implements GoodsService{
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private TypeMapper typeMapper;
    @Autowired
    private TypeService typeService;

    @Override
    public boolean insert(Goods goods) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("name",goods.getName());
        List<Brand> brands = goodsMapper.selectList(queryWrapper);
        if(brands.size() > 0){
            return false;
        }
        goodsMapper.insert(goods);
        return true;
    }

    @Override
    public boolean delete(Long id) {
        String url=goodsMapper.selectById(id).getImage();
        String key= StringUtil.getKey(url);
        if(goodsMapper.deleteById(id) < 0){
            return false;
        }
        COSStorage.delete(key);
        return true;
    }

    @Override
    public boolean update(Goods goods) {
        Long id =goods.getId();
        String url=goodsMapper.selectById(id).getImage();
        String key= StringUtil.getKey(url);
        COSStorage.delete(key);
        if (goodsMapper.updateById(goods) > 0){
            return true;
        }
        return false;
    }

    @Override
    public IPage<GoodsVO> getGoodsList(GoodsKeyWordDTO goodsKeyWordDTO) {
        Page<GoodsVO> page = new Page<>();
        goodsKeyWordDTO.setCurrentPage((goodsKeyWordDTO.getCurrentPage()-1)*goodsKeyWordDTO.getPageSize());
        List<GoodsVO> goodsList=goodsMapper.getGoodsList(goodsKeyWordDTO);
        page.setRecords(goodsList);
        page.setCurrent(goodsKeyWordDTO.getCurrentPage());
        page.setSize(goodsKeyWordDTO.getPageSize());
        int count=goodsMapper.getGoodsTotal(goodsKeyWordDTO);
        page.setTotal(count);
        return page;
    }

    @Override
    public List<Goods> getGoodsByType(Long id) {
        List<Long> typeIds=new ArrayList<>();
        QueryWrapper queryWrapper=new QueryWrapper();
        List<Type> typeList=typeMapper.selectList(queryWrapper);
        for (Type type:typeList){
            if (type.getId().equals(id)){
                typeIds.add(type.getId());
                setChildrenTypes(type,typeIds,typeList);
            }
        }
        System.out.println(typeIds);
        queryWrapper=new QueryWrapper();
        List<Goods> goodsList=goodsMapper.selectList(queryWrapper);
        List<Goods> resultList=new ArrayList<>();
        for(Long typeId:typeIds){
           for (Goods goods:goodsList){
               if (goods.getTypeId().equals(typeId)){
                   resultList.add(goods);
               }
           }
        }
        return resultList;
    }

    public void setChildrenTypes(Type type,List<Long> typeIds,List<Type> typeList){
        for (Type type1:typeList){
            if (type1.getParentId().equals(type.getId())){
                typeIds.add(type1.getId());
                setChildrenTypes(type1,typeIds,typeList);
            }
        }
    }
}
